package com.cskaoyan.service;

import com.cskaoyan.bean.Goods;
import com.cskaoyan.bean.GrouponRules;
import com.cskaoyan.bean.GrouponRulesExample;
import com.cskaoyan.bo.GrouponCreateBo;
import com.cskaoyan.bo.GrouponDeleteBo;
import com.cskaoyan.bo.GrouponUpdateBo;
import com.cskaoyan.mapper.GoodsMapper;
import com.cskaoyan.mapper.GrouponRulesMapper;
import com.cskaoyan.vo.GrouponRulesVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class GrouponRulesServicelmpl implements  GrouponRulesService {

    @Autowired
    GrouponRulesMapper grouponRulesMapper;
    @Autowired
    GoodsMapper goodsMapper;

    /**
     * 功能:团购规则页面
     * 作者:whj
     */
    @Override
    public GrouponRulesVo query(Integer page, Integer limit, Integer goodsId, String sort, String order) {
        //分页
        PageHelper.startPage(page,limit);
        //排序
        GrouponRulesExample grouponRulesExample = new GrouponRulesExample();
        grouponRulesExample.setOrderByClause(sort + " " + order);
        //根据userId,valueId进行精准查询
        GrouponRulesExample.Criteria criteria = grouponRulesExample.createCriteria();
        if(goodsId != null && !"".equals(goodsId)){
            criteria.andGoodsIdEqualTo(goodsId);
        }
        //剔除逻辑删除的管理员
        criteria.andDeletedEqualTo(false);
        List<GrouponRules> grouponRules = grouponRulesMapper.selectByExample(grouponRulesExample);
        //符合条件的total
        PageInfo<GrouponRules> grouponRulesPageInfo = new PageInfo<>(grouponRules);
        long total = grouponRulesPageInfo.getTotal();
        //返回结果
        GrouponRulesVo grouponRulesVo = new GrouponRulesVo();
        grouponRulesVo.setItems(grouponRules);
        grouponRulesVo.setTotal(total);
        return grouponRulesVo;
    }

    /**
     * 功能:团购规则页面,/groupon/create
     * 作者:whj
     */
    @Transactional(isolation = Isolation.REPEATABLE_READ,propagation = Propagation.REQUIRED)
    @Override
    public GrouponRules create(GrouponCreateBo grouponCreateBo) {
        GrouponRules grouponRules = new GrouponRules();
        int goodsId = Integer.parseInt(grouponCreateBo.getGoodsId());
        grouponRules.setGoodsId(goodsId);
        grouponRules.setDiscount(new BigDecimal(grouponCreateBo.getDiscount()));
        grouponRules.setDiscountMember(Integer.parseInt(grouponCreateBo.getDiscountMember()));
        //String转为Date
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date expireTime = null;
        try {
            expireTime = dateFormat.parse(grouponCreateBo.getExpireTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        grouponRules.setExpireTime(expireTime);
        //根据goodsId查询
        Goods goods = goodsMapper.selectByPrimaryKey(goodsId);
        grouponRules.setGoodsName(goods.getName());
        grouponRules.setPicUrl(goods.getPicUrl());
        grouponRules.setAddTime(new Date(System.currentTimeMillis()));
        grouponRules.setUpdateTime(new Date(System.currentTimeMillis()));
        grouponRules.setDeleted(false);
        grouponRulesMapper.insertBackId(grouponRules);
        grouponRules.setDeleted(null);
        return grouponRules;
    }

    /**
     * 功能:团购规则页面,/groupon/update
     * 作者:whj
     */
    @Override
    public void update(GrouponUpdateBo grouponUpdateBo) {
        GrouponRules grouponRules = new GrouponRules();
        grouponRules.setId(grouponUpdateBo.getId());
        grouponRules.setGoodsId(grouponUpdateBo.getGoodsId());
        grouponRules.setDiscount(new BigDecimal(grouponUpdateBo.getDiscount()));
        grouponRules.setDiscountMember(grouponUpdateBo.getDiscountMember());
        //String转为Date
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date expireTime = null;
        Date addTime = null;
        try {
            expireTime = dateFormat.parse(grouponUpdateBo.getExpireTime());
            addTime = dateFormat.parse(grouponUpdateBo.getAddTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        grouponRules.setExpireTime(expireTime);
        grouponRules.setAddTime(addTime);
        //根据goodsId查询
        Goods goods = goodsMapper.selectByPrimaryKey((grouponUpdateBo.getGoodsId()));
        grouponRules.setGoodsName(goods.getName());
        grouponRules.setPicUrl(goods.getPicUrl());
        grouponRules.setUpdateTime(new Date(System.currentTimeMillis()));
        grouponRules.setDeleted(false);
        grouponRulesMapper.updateByPrimaryKey(grouponRules);
        grouponRules.setDeleted(null);
    }

    /**
     * 功能:团购规则页面,/groupon/delete
     * 作者:whj
     */
    @Override
    public void delete(GrouponDeleteBo grouponDeleteBo) {
        GrouponRules grouponRules = new GrouponRules();
        grouponRules.setId(grouponDeleteBo.getId());
        int goodsId = Integer.parseInt(grouponDeleteBo.getGoodsId());
        grouponRules.setGoodsId(goodsId);
        grouponRules.setDiscount(new BigDecimal(grouponDeleteBo.getDiscount()));
        grouponRules.setDiscountMember(grouponDeleteBo.getDiscountMember());
        //String转为Date
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date expireTime = null;
        Date addTime = null;
        try {
            expireTime = dateFormat.parse(grouponDeleteBo.getExpireTime());
            addTime = dateFormat.parse(grouponDeleteBo.getAddTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        grouponRules.setExpireTime(expireTime);
        grouponRules.setAddTime(addTime);
        //根据goodsId查询
        Goods goods = goodsMapper.selectByPrimaryKey(goodsId);
        grouponRules.setGoodsName(goods.getName());
        grouponRules.setPicUrl(goods.getPicUrl());
        grouponRules.setUpdateTime(new Date(System.currentTimeMillis()));
        grouponRules.setDeleted(true);
        grouponRulesMapper.updateByPrimaryKey(grouponRules);
        grouponRules.setDeleted(null);
    }
}
